package com.bidevalution.business.integrate;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.function.FunctionCalculator;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.util.StringUtil;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 计算平均值
 * Created by glodon on 2019-10-09.
 */
public class ScoreComputerResultJob implements StepJobJdbcFactory {
    private JdbcTemplateService jdbcTemplateService;

    @Override
    public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode,String expertId,String layouCode)
            throws Exception {
    	if(!StringUtils.isEmpty(beSectionStepJob.getJobContent())) {
    		 String[] params = StringUtils.delimitedListToStringArray(beSectionStepJob.getJobContent(), "|");
				//c1|c3 c1表示需要计算的字段 c3表示最终结果字段
    	        String[] columnCodes = StringUtils.delimitedListToStringArray(params[0],",");

    	        Map<String, Object> paramMap = new HashMap<String, Object>();
    	        paramMap.put("sectionStepId", beSectionStepJob.getSectionStepId());
    	        paramMap.put("submitCode", submitCode);
    	        List<String> columnValueList = new ArrayList<String>();
    	        List<Map<String, Object>> dataList;
    	        Map<String, Object> env;
    	        //获取评审环节的企业列表
    	        for (String enterpriseId : this.jdbcTemplateService.selectForList(
    	                "select DISTINCT enterprise_id from be_evalution_outcome where section_step_id=:sectionStepId and is_del='0'",
    	                paramMap, String.class)) {
    	            paramMap.put("enterpriseId", enterpriseId);
    	            //获取评审数据,isFinish表示提交的。这样加在条件是为了实现只有组长的才有打分资格
    	            dataList = this.jdbcTemplateService.selectForList(
    	                    "select c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20 " +
									"from be_evalution_outcome where section_step_id=:sectionStepId and is_del='0' and enterprise_id=:enterpriseId and is_finish='1'",
    	                    paramMap);
    	            //计算所有专家对需要计算平均值字段的结果，计算平均值
    	            for (int i = 0; i < columnCodes.length; i++) {
    	                columnValueList = new ArrayList<String>();
    	                for (Map<String, Object> data : dataList) {
							columnValueList.add(StringUtil.toString(data.get(columnCodes[i])));
    	                }
    	                env = new HashMap<String, Object>();
    	                env.put("columnValues", StringUtils.collectionToDelimitedString(columnValueList, ","));
    	                //计算平均值
    	                String result = FunctionCalculator.computeAviator("average(columnValues)", env);
    	                this.jdbcTemplateService.update("update be_evalution_outcome set "+params[1]+"='"+result+"' where section_step_id=:sectionStepId and is_del='0' and enterprise_id=:enterpriseId", paramMap);
    	            }
    	        }
    	}
       
        return true;
    }

    @Override
    public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
        this.jdbcTemplateService = jdbcTemplateService;
    }

}
